﻿using Engine.FileFormats3D;
using System;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace ModelViewerTabs.TabsForModelviewer
{
    public partial class ImportToolbox : UserControl
    {
        #region Private Fields

        private string modelPath = string.Empty;
        private ModelViewer.ModelViewerForm modelViewer;
        private OpenFileDialog ofdModel = new OpenFileDialog();
        private OpenFileDialog ofdShape = new OpenFileDialog();
        private string shapePath = string.Empty;

        #endregion Private Fields

        #region Public Constructors

        public ImportToolbox()
        {
            InitializeComponent();
            this.ofdModel.Filter = "Wavefront OBJ-File|*.obj";
            this.ofdShape.Filter = "Wavefront OBJ-File|*.obj";
        }

        #endregion Public Constructors

        #region Internal Methods

        internal void SetModelViewerReference(ModelViewer.ModelViewerForm modelviewer)
        {
            this.modelViewer = modelviewer;
        }

        #endregion Internal Methods

        #region Private Methods

        private void button_ImportModel_Click(object sender, EventArgs e)
        {
            if (ofdModel.ShowDialog() == DialogResult.OK)
            {
                this.modelPath = this.ofdModel.FileName;
                this.button_ImportModel.Text = Path.GetFileNameWithoutExtension(this.modelPath);
                this.button_ImportModel.BackColor = Color.LightGreen;

                if (this.modelViewer != null)
                {
                    LoadMesh();
                }
                else
                {
                    throw new Exception("No model loaded in import tab!");
                }
            }
        }

        private void button_ImportShape_Click(object sender, EventArgs e)
        {
            if (this.ofdShape.ShowDialog() == DialogResult.OK)
            {
                this.shapePath = this.ofdShape.FileName;

                if (this.modelViewer != null)
                {
                    LoadShape();
                    this.button_ImportShape.Text = Path.GetFileNameWithoutExtension(this.shapePath);
                    this.button_ImportShape.BackColor = Color.LightGreen;
                }
                else
                {
                    throw new Exception("No model loaded in import tab!");
                }
            }
        }

        private void button_ReloadModel_Click(object sender, EventArgs e)
        {
            LoadMesh();
        }

        private void button_ReloadShape_Click(object sender, EventArgs e)
        {
            LoadShape();
        }

        private void button_RemoveModel_Click(object sender, EventArgs e)
        {
            if (this.modelViewer.model.ScenerieObject.Mesh != null)
                this.modelViewer.model.ScenerieObject.Mesh.Dispose();
            this.modelViewer.model.ScenerieObject.Mesh = null;

            this.button_ImportModel.BackColor = Color.LightPink;
            this.button_ImportModel.Text = "Import Model";
        }

        private void button_RemoveShape_Click(object sender, EventArgs e)
        {
            if (this.modelViewer.model.ScenerieObject.Shape != null)
                this.modelViewer.model.ScenerieObject.Shape.Dispose();
            this.button_ImportShape.BackColor = Color.LightPink;
            this.button_ImportShape.Text = "Import Shape";
        }

        private void LoadMesh()
        {
            if (File.Exists(this.modelPath))
            {
                WavefrontObjects wavefront = WavefrontObjects.FromFile(this.modelPath);
                this.modelViewer.model.SetMesh(wavefront);
                this.modelViewer.materialListUI.SetMaterialManager(this.modelViewer.model.ScenerieObject.Mesh.MaterialManager, null);
            }
        }

        private void LoadShape()
        {
            if (File.Exists(this.shapePath))
            {
                WavefrontObjects wavefront = WavefrontObjects.FromFile(this.shapePath);
                this.modelViewer.model.SetShape(wavefront);
            }
        }

        #endregion Private Methods
    }
}